package com.lei.zjs.web.action;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.crm.domain.Customer;
import cn.itcast.crm.service.CustomerService;

import com.lei.zjs.service.ebi.DecidezoneService;
import com.lei.zjs.vo.Decidedzone;
import com.lei.zjs.vo.Staff;
import com.lei.zjs.web.action.base.BaseAction;

/**
 * 定区管理Action
 * @author Spinacia
 *
 */
@Controller
@Scope("prototype")
public class DecidezoneAction extends BaseAction<Decidedzone> {

	@Resource
	private DecidezoneService decidezoneService;
	
	private String[] subareaids;
	
	public void setSubareaids(String[] subareaids) {
		this.subareaids = subareaids;
	}


	//保存定区信息
	public String save(){
		decidezoneService.save(model,subareaids);
		return "list";
	}
	
	//分页查询定区信息
	public String pageQuery(){
		DetachedCriteria decidezoneDC = pageBean.getDetachedCriteria();
		String id = model.getId();
		if (StringUtils.isNotBlank(id)) {
			decidezoneDC.add(Restrictions.like("id", "%"+id+"%"));
		}
		Staff staff = model.getStaff();
		if (staff != null) {
			DetachedCriteria staffDC = decidezoneDC.createCriteria("staff");
			String station = staff.getStation();
			if (StringUtils.isNotBlank(station)) {
				staffDC.add(Restrictions.like("station", "%"+station+"%"));
			}
		}
		
		decidezoneService.pageQuery(pageBean);
		String[] excludes = new String[]{"currentPage", "pageSize",
				"detachedCriteria","subareas","decidedzones"};
		try {
			this.writeJsonObject2String(pageBean, excludes );
		} catch (IOException e) {
			e.printStackTrace();
		}
		return NONE;
	}
	
	//注入远程调用服务
	@Resource
	private CustomerService customerService;
	//查找没有与定区关联的用户
	public String findCustomerNoassociationSelect() throws IOException{
		List<Customer> list = customerService.findnoassociationCustomers();
		String[] excludes = new String[]{"station","telephone","address","decidedzone_id"};
		this.writeJsonArray2String(list,excludes);
		return NONE;
	}
	//查找已经与定区关联的用户
	public String findCustomerAssociationSelect() throws IOException{
		List<Customer> list = customerService.findhasassociationCustomers(model.getId());
		String[] excludes = new String[]{"station","telephone","address","decidedzone_id"};
		this.writeJsonArray2String(list,excludes);
		return NONE;
	}
	//属性驱动用于接收关联用户的id
	private Integer[] customerIds;
	
	public void setCustomerIds(Integer[] customerIds) {
		this.customerIds = customerIds;
	}

	//定区关联用户
	public String assigncustomerstodecidedzone(){
		customerService.assignCustomersToDecidedZone(customerIds, model.getId());
		return "list";
	}
}
